package com.chat.datea.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.chat.datea.R;
import com.chat.datea.adapter.SingerGridViewAdapter;
import com.chat.datea.bean.HotSingerEntity;
import com.chat.datea.utils.fragment.ReplaceFragment;
import com.chat.datea.utils.volley.VolleySingleton;
import com.google.gson.Gson;

import java.util.List;

/**
 * Created by Shura on 15/10/27.
 */
public class HotSingerFragment extends Fragment {
    private GridView gridView;
    private ImageView back;
    private VolleySingleton volleySingleton;
    private SingerGridViewAdapter singerGridViewAdapter;
    private List<HotSingerEntity.ArtistEntity> artistEntities;
    private String url = "http://tingapi.ting.baidu.com/" +
            "v1/restserver/ting?from=android&version=5.6.5.0&method=baidu.ting.artist.getList&format=json&offset=0&limit=48&order=1&area=0&sex=0";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        volleySingleton = VolleySingleton.getVolleySingleton(getActivity());
        View view = inflater.inflate(R.layout.singer_gridview, null);
        initView(view);
        getData();
        return view;
    }

    private void initView(View view) {
        gridView = (GridView) view.findViewById(R.id.singer_gridView);
        back = (ImageView) view.findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().getSupportFragmentManager().popBackStack();
            }
        });
    }

    private void getData() {
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                HotSingerEntity hotSingerEntity = gson.fromJson(response, HotSingerEntity.class);
                artistEntities = hotSingerEntity.getArtist();
                singerGridViewAdapter = new SingerGridViewAdapter(getActivity(), artistEntities);
                gridView.setAdapter(singerGridViewAdapter);
                gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                        GuessSingerFragment singerFragment = new GuessSingerFragment(getActivity().getSupportFragmentManager(), artistEntities.get(position).getTing_uid(), artistEntities.get(position).getArtist_id(), artistEntities.get(position).getName());
                        new ReplaceFragment(singerFragment,getActivity().getSupportFragmentManager(),R.id.fl_content,true,null);
                    }
                });
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(request, "HotSingerFragment");
    }
}

